80: def find_top(find_type, options = {})
81: raise_on_illegal_options(
82: options, :order, :joins, :include, :select, :group
83: )
84:
85: joins_sql = "
86: LEFT JOIN VOTES ON
87: voteable_id = #{table_name}.id AND
88: voteable_type = ?
89: "
90: joins_params = [name]
91:
92: if since = options.delete(:since)
93: joins_sql << "AND votes.created_at >= ? "
94: joins_params << since
95: end
96:
97: if about = options.delete(:about)
98: joins_sql << "AND votes.about = ? "
99: joins_params << about
100: end
101:
102: joins = sanitize_sql(joins_params.unshift(joins_sql))
103:
104: options.merge!(
105: :select => "#{table_name}.id",
106: :joins => joins,
107: :group => "#{table_name}.id",
108: :order => "sum(CASE WHEN votes.points IS NULL THEN 0 ELSE votes.points END) DESC"
109: )
110:
111: result = find(find_type, options)
112: if find_type.to_s == 'all'
113: result.map!{|x| x.reload}
114: end
115: result
116: end